package com.ruoyi.jiaopei.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jiaopei.domain.bo.TbClassStudentBo;
import com.ruoyi.jiaopei.domain.vo.TbClassStudentVo;
import com.ruoyi.jiaopei.domain.TbClassStudent;
import com.ruoyi.jiaopei.mapper.TbClassStudentMapper;
import com.ruoyi.jiaopei.service.ITbClassStudentService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 班级学员Service业务层处理
 *
 * @author macy
 * @date 2024-04-03
 */
@RequiredArgsConstructor
@Service
public class TbClassStudentServiceImpl implements ITbClassStudentService {

    private final TbClassStudentMapper baseMapper;

    /**
     * 查询班级学员
     */
    @Override
    public TbClassStudentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public Long selectCount(Wrapper<TbClassStudent> queryWrapper) {
        return baseMapper.selectCount(queryWrapper);
    }


    /**
     * 查询班级学员列表
     */
    @Override
    public TableDataInfo<TbClassStudentVo> queryPageList(TbClassStudentBo bo, PageQuery pageQuery) {
        QueryWrapper<TbClassStudent> lqw = buildQueryWrapper2(bo);
        Page<TbClassStudentVo> result = baseMapper.selectPageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询班级学员列表
     */
    @Override
    public List<TbClassStudentVo> queryList(TbClassStudentBo bo) {
        LambdaQueryWrapper<TbClassStudent> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<TbClassStudent> buildQueryWrapper2(TbClassStudentBo bo) {

        QueryWrapper<TbClassStudent> lqw = Wrappers.query();
        lqw.eq(bo.getClassId() != null, "t.class_id", bo.getClassId());
        lqw.eq(bo.getStudentId() != null,"t.student_id", bo.getStudentId());
        lqw.eq(bo.getCurriculumId() != null,"t.curriculum_id", bo.getCurriculumId());
        lqw.eq(StringUtils.isNotBlank(bo.getState()),"t.state", bo.getState());

        lqw.like(StringUtils.isNotBlank(bo.getStudentName()),"u.name", bo.getStudentName());
        lqw.like(StringUtils.isNotBlank(bo.getCurriculumName()),"c.name", bo.getCurriculumName());
        lqw.like(StringUtils.isNotBlank(bo.getClassName()),"b.name", bo.getClassName());


        return lqw;
    }


    private LambdaQueryWrapper<TbClassStudent> buildQueryWrapper(TbClassStudentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbClassStudent> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getClassId() != null, TbClassStudent::getClassId, bo.getClassId());
        lqw.eq(bo.getStudentId() != null, TbClassStudent::getStudentId, bo.getStudentId());
        lqw.eq(bo.getCurriculumId() != null, TbClassStudent::getCurriculumId, bo.getCurriculumId());
        lqw.eq(StringUtils.isNotBlank(bo.getState()), TbClassStudent::getState, bo.getState());
        return lqw;
    }

    /**
     * 新增班级学员
     */
    @Override
    public Boolean insertByBo(TbClassStudentBo bo) {
        TbClassStudent add = BeanUtil.toBean(bo, TbClassStudent.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改班级学员
     */
    @Override
    public Boolean updateByBo(TbClassStudentBo bo) {
        TbClassStudent update = BeanUtil.toBean(bo, TbClassStudent.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbClassStudent entity){
        if(null==entity.getId()){
            entity.setCreatedDate(new Date());
            long count=baseMapper.selectCount(new LambdaQueryWrapper<TbClassStudent>()
                .eq(TbClassStudent::getClassId,entity.getClassId())
                .eq(TbClassStudent::getStudentId,entity.getStudentId())
            );

            if(count>0){
                throw new ServiceException("学员已存在,无需添加...");
            }
        }
    }

    /**
     * 批量删除班级学员
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
